<!DOCTYPE html "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
        <title>Yahoo Maps Geocoverage</title>
        <script type="text/javascript" src="jquery-1.2.6.min.js"></script>
        <script type="text/javascript" src="wscaller.js"></script>
        <script type="text/javascript" src="http://api.maps.yahoo.com/ajaxymap?v=3.8&appid=h0KrECTIkY0MfDX0ownAdzXs_mK6e0hBrtLl "></script>    
        <script type="text/javascript">  
var i=25;
$(document).ready(function()
{
    $.ajaxSetup({ global: false, type: "GET", url: "http://192.168.0.16:8080/geoserver/ows" });
    map = new YMap(document.getElementById('mapdiv'));  
    processNextCity();
});

function processNextCity()     
{  
    if(i==0)
    {
        location.reload() ; 
    }
    i=i-1;
    GetNextCity("YM", function(guid, y, x)
    {
        processZoomLevel(guid, y, x, 1,'a');
    }, LogMsg);
}

function processZoomLevel(guid, y, x, zoom,typemap)
{
    $("#mapdiv").css("width","50px");
    $("#mapdiv").css("height","50px");
    map.drawZoomAndCenter(new YGeoPoint(y, x), zoom);
     if (typemap == 'a')
        map.setMapType(YAHOO_MAP_SAT);
    else 
        map.setMapType(YAHOO_MAP_REG);    
    if(typemap == 'r' && zoom == 2)
    {
        $("#mapdiv").css("width","800px");
        $("#mapdiv").css("height","800px");
    }
    window.setTimeout(processMap, 2000, guid, zoom, typemap, function()
    {
        if (typemap =='a')
        {
            typemap = 'r';
        }
        else
        {
            zoom = zoom+1;
            typemap = 'a';
        }
        if(zoom<11)
        {
            processZoomLevel(guid, y, x, zoom, typemap);
        }
        else
        {
            processNextCity();
        }
    }, 10);         
}

function processMap(guid, zoom, typemap, callback, retry_count)
{
    tiles = '';
    if (typemap == 'a' || zoom == 2)
    {
        tilestable=GetTiles(typemap);
        if(typemap == 'r' && zoom == 2)
            nb_expected_tiles=16;
        else
            nb_expected_tiles=1;
        if (tilestable.length >= nb_expected_tiles ) 
            tiles=tilestable.join("|");
    }
    LogMsg("tiles=##### "+tiles+" ####### typemap=" + typemap+" zoom="+ zoom);
    if (tiles == '' && retry_count>0 && (typemap == 'a' || zoom == 2))
    {
         window.setTimeout(processMap, 1000, guid, zoom,typemap, callback, retry_count-1); //passing parameters to setTimeOut works only in FireFox
    }
    else if(retry_count>0)
    {
        if (typemap=='a')
        {
            tabcp = GetCopyrightPhoto();
            res = GetResolution();
            cp ='';
            if (tabcp.length>0)
            {
                cp=tabcp.join("|");
            }
            SendToServer(guid, zoom, typemap, tiles, cp, res, callback);
        }
        else
        {
            tabcp = GetCopyright();
            cp ='';
            if (tabcp.length>0)
            {
                cp=tabcp.join("|");
            }
            SendToServer(guid, zoom, typemap, tiles, cp, '', callback);
        } 
    }
    else
    {
        LogMsg("Tiles did not load after retries !");
        callback();
    }
}

function GetTiles()
{
    tab = new Array();
    j=0;
    $("#mapdiv > div:first > img").each(function()
    {
        tab[j++]=$(this).attr("src");
    });
    return tab;
}    


function GetCopyright()
{
    tab = new Array();
    j=0;
    $("#mapdiv > div:last > img").each(function()
    {
        tab[j++]=$(this).attr("src");
    });
    return tab;
}    

function GetCopyrightPhoto()
{
    tab = new Array();
    j=0;
    $("#mapdiv > img:last").each(function()
    {
        tab[j++]=$(this).attr("src");
    });
    return tab;
}
    
function GetResolution()
{
    view = map.getBoundsLatLon();
    bottomright = view.LatMin;
    topleft = view.LatMax;

    res = ((topleft - bottomright)*60*1852)/800;
    return res;
}
      
function SendToServer(guid, zoom, typemap,tiles, cp, res, callback)
{
    SendTilesToServer(guid, zoom,typemap, tiles, function()
    {
        SendCopyrTilesToServer(guid, zoom, typemap, cp, function()
        {
            SendResultToServer(guid, zoom, typemap, 4, res, 2, function()
            {
                 callback();
            }, LogMsg );
        }, LogMsg );
    }, LogMsg );
}
        </script>
    </head>
    <body>  
        <div id='mapdiv' style="position:relative; width:50px; height:50px;"></div>
    </body>  
</html>  
